<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/4/8
 * Time: 9:02
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Consumer extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($sex)&&$sex!="")
            {
                $map['a.user_gender'] = $sex;
            }
            if(isset($keyword) && $keyword != "")
            {
                $where['a.user_username|a.user_phone'] = ['like', "%" . $keyword . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.user_time'] = ['between time',[$start,$end]];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.user_sheng'] = $province;
            }

            if(isset($city)&&$city!="")
            {
                $map['a.user_shi'] = $city;
            }
            if(isset($district)&&$district!="")
            {
                $map['a.user_qu'] = $district;
            }
            if(isset($status)&&$status!="")
            {
                $map['a.user_status'] = $status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_u_user')->alias('a')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_u_user')->alias('a')
                ->page($NowPage,$limits)
                ->order('user_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['user_gender']==1){
                    $vo['gender'] = '男';
                }elseif ($vo['user_gender']==2){
                    $vo['gender'] = '女';
                }
                if($vo['user_status']==1){
                    $vo['user_status_name'] = '未认证';
                }elseif ($vo['user_status']==2){
                    $vo['user_status_name'] = '已认证';
                }elseif ($vo['user_status']==3){
                    $vo['user_status_name'] = '冻结';
                }
                $sheng = Db::table('up_area')->where('district_id',$vo['user_sheng'])->value('district');
                $shi = Db::table('up_area')->where('district_id',$vo['user_shi'])->value('district');
                $qu = Db::table('up_area')->where('district_id',$vo['user_qu'])->value('district');
                $vo['address'] = $sheng.$shi.$qu;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *
     */
    public function allow(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',2);
        if($res){
            //推送
            $list1 = [];
            $v = (string)$task_id;
            array_push($list1, $v);
            $str = array('msg'=>"您的认证已经通过",'type'=>10,'user_id'=>$task_id);
            $str1 = "您的认证已经通过申请";
            try{
                $this->Jpush1(1, $list1, $str1, $str);
            }catch(\Exception $e){

            }
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     *
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',3);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /**
     * 充值
     */
    public function recharge(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $gold =input('gold');
            $res = Db::table('up_u_user')->where('user_id',$id)->setInc('user_balance',$gold);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    /**
     * 扣款
     */
    public function deduction(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $gold =input('gold');
            $user_balance = Db::table('up_u_user')->where('user_id',$id)->value('user_balance');
            if($gold>=$user_balance){
                return json(['code' => 100, 'msg' => '金币不足,请重新输入']);
            }
            $res = Db::table('up_u_user')->where('user_id',$id)->setDec('user_balance',$gold);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }

    /**
     * @return mixed
     */
    public function detail(){
        $id = input('param.id');
        $res = Db::table('up_u_user')->where('user_id',$id)->find();
        if($res['user_smoke']==1){
            $res['smoke'] = '从不吸烟';
        }elseif ($res['user_smoke']==2){
            $res['smoke'] = '偶尔一根';
        }elseif ($res['user_smoke']==3){
            $res['smoke'] = '戒烟中';
        }elseif ($res['user_smoke']==4){
            $res['smoke'] = '老烟枪';
        }else{
            $res['smoke'] = '';
        }
        if($res['user_liquor']==1){
            $res['liquor'] = '滴酒不沾';
        }elseif ($res['user_liquor']==2){
            $res['liquor'] = '偶尔小酌';
        }elseif ($res['user_liquor']==3){
            $res['liquor'] = '社交应酬';
        }elseif ($res['user_liquor']==4){
            $res['liquor'] = '品酒达人';
        }elseif ($res['user_liquor']==5){
            $res['liquor'] = '千杯不醉';
        }elseif ($res['user_liquor']==6){
            $res['liquor'] = '一杯脸红';
        }else{
            $res['liquor'] = '';
        }
        $this->assign('res',$res);
        return $this->fetch();
    }
}